import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

/**
 * 遇到`Error: Avoided redundant navigation to current location:`报错显示是路由重复，
 * 使用下列方法即可。
 */
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

var router = new Router({
    routes: [
        {
            path: '/',
            redirect: '/dashboard'
        },
        {
            path: '/',
            /* webpackChunkName: "home" */
            component: () => import('../components/common/Home.vue'),
            meta: { title: '自述文件' },
            children: [
                {
                    path: '/dashboard',
                    /* webpackChunkName: "dashboard" */
                    component: () => import( '../components/page/Dashboard.vue'),
                    meta: { title: '系统首页' }
                },
                {
                    path: '/userinfo',
                    component: () => import('@/components/admin/UserInfo.vue'),
                    meta: { title: '用户信息设置' }
                },
                {
                    path: '/icon',
                    /* webpackChunkName: "icon" */ 
                    component: () => import('../components/page/Icon.vue'),
                    meta: { title: '自定义图标' }
                },
                {
                    path: '/table',
                    /* webpackChunkName: "table" */ 
                    component: () => import('../components/page/BaseTable.vue'),
                    meta: { title: '基础表格' }
                },
                {
                    path: '/tabs',
                    /* webpackChunkName: "tabs" */ 
                    component: () => import('../components/page/Tabs.vue'),
                    meta: { title: 'tab选项卡' }
                },
                {
                    path: '/form',
                    /* webpackChunkName: "form" */ 
                    component: () => import('../components/page/BaseForm.vue'),
                    meta: { title: '基本表单' }
                },
                {
                    // 富文本编辑器组件
                    path: '/editor',
                    /* webpackChunkName: "editor" */ 
                    component: () => import('../components/page/VueEditor.vue'),
                    meta: { title: '富文本编辑器' }
                },
                {
                    // markdown组件
                    path: '/markdown',
                    /* webpackChunkName: "markdown" */ 
                    component: () => import('../components/page/Markdown.vue'),
                    meta: { title: 'markdown编辑器' }
                },
                {
                    // 图片上传组件
                    path: '/upload',
                    /* webpackChunkName: "upload" */ 
                    component: () => import('../components/page/Upload.vue'),
                    meta: { title: '文件上传' }
                },
                {
                    // vue-schart组件
                    path: '/charts',
                    /* webpackChunkName: "chart" */ 
                    component: () => import('../components/page/BaseCharts.vue'),
                    meta: { title: 'schart图表' }
                },
                {
                    // 拖拽列表组件
                    path: '/drag',
                    /* webpackChunkName: "drag" */ 
                    component: () => import('../components/page/DragList.vue'),
                    meta: { title: '拖拽列表' }
                },
                {
                    // 拖拽Dialog组件
                    path: '/dialog',
                    /* webpackChunkName: "dragdialog" */ 
                    component: () => import('../components/page/DragDialog.vue'),
                    meta: { title: '拖拽弹框' }
                },
                {
                    // 国际化组件
                    path: '/i18n',
                    /* webpackChunkName: "i18n" */ 
                    component: () => import('../components/page/I18n.vue'),
                    meta: { title: '国际化' }
                },
                {
                    // 权限页面
                    path: '/permission',
                    /* webpackChunkName: "permission" */
                    component: () => import('../components/page/Permission.vue'),
                    meta: { title: '权限测试', permission: true }
                },
                {
                    path: '/404',
                    /* webpackChunkName: "404" */
                    component: () => import( '../components/page/404.vue'),
                    meta: { title: '404' }
                },
                {
                    path: '/403',
                    /* webpackChunkName: "403" */
                    component: () => import( '../components/page/403.vue'),
                    meta: { title: '403' }
                },
                {
                    path: '/donate',
                    /* webpackChunkName: "donate" */ 
                    component: () => import('../components/page/Donate.vue'),
                    meta: { title: '支持作者' }
                },
            ]
        },
        {
            path: '/login',
            component: () => import(/* webpackChunkName: "login" */ '../components/page/Login.vue'),
            meta: { title: '登录' }
        },
        {
            path: '/window404',
            /* webpackChunkName: "404" */
            component: () => import( '@c/page/404.vue'),
            meta: { title: '404' }
        },
    ],
    
});

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
    document.title = `海思特CRO管理系统`;
    const role = localStorage.getItem('ms_username');
    // console.log(to.path+","+!role +","+(to.path !== '/login'))
    if (!role && to.path !== '/login' && to.path != "/window404") {
        next('/login');
    } 
    else{
        next();
    }
    // else if (to.meta.permission) {
    //     // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
    //     role === 'admin' ? next() : next('/403');
    // } else {
    //     // 简单的判断IE10及以下不进入富文本编辑器，该组件不兼容
    //     if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
    //         Vue.prototype.$alert('vue-quill-editor组件不兼容IE10及以下浏览器，请使用更高版本的浏览器查看', '浏览器不兼容通知', {
    //             confirmButtonText: '确定'
    //         });
    //     } else {
    //         next();
    //     }
    // }
});
export default router